﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nikola.Common
{
    public class CookieManager
    {
        #region Private Methods
        /// <summary>
        /// 得到Cookie值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        private static string GetCookieValue(string key)
        {
            if ((HttpContext.Current.Request.Cookies[key] != null))
            {
                string value = HttpContext.Current.Request.Cookies[key].Value;
                if (!string.IsNullOrWhiteSpace(value))
                    value = CommonHelper.DecryptDES(value);
                return value;
            }
            return string.Empty;
        }

        #endregion

        #region Public Methods
        /// <summary>
        /// 移除cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static bool RemoveCookieValue(string key)
        {
            bool flag = false;
            if ((HttpContext.Current.Request.Cookies[key] != null))
            {
                HttpContext.Current.Response.Cookies[key].Expires = DateTime.Now.AddDays(-1);
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 设置cookice
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expiration">过期时间，以天为单位</param>
        public static void SetCookieValue(string key, string value, int expiration = 0)
        {
            if (!string.IsNullOrWhiteSpace(value))
                value = CommonHelper.EncryptDES(value);

            if ((HttpContext.Current.Request.Cookies[key] != null))
            {
                HttpContext.Current.Response.Cookies[key].Value = value;
                if (expiration != 0)
                    HttpContext.Current.Response.Cookies[key].Expires = DateTime.Now.AddDays(Convert.ToDouble(expiration));
            }
            else
            {
                HttpCookie Cookie = new HttpCookie(key, value);
                if (expiration != 0)
                    Cookie.Expires = DateTime.Now.AddDays(Convert.ToDouble(expiration));
                HttpContext.Current.Response.Cookies.Add(Cookie);
            }
        }
        /// <summary>
        /// 得到cookice值，以字符串返回
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">如果为空，返回此默认值</param>
        /// <returns></returns>
        public static string GetCookieAsString(string key, string DefaultValue = "")
        {
            string Result = GetCookieValue(key);
            if (Result != string.Empty)
            {
                return Result;
            }
            return DefaultValue;
        }

        /// <summary>
        /// 得到cookice值，以bool返回
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">如果为空，返回此默认值</param>
        /// <returns></returns>
        public static bool GetCookieAsBool(string key, bool DefaultValue)
        {
            string Result = GetCookieValue(key);
            if (Result != string.Empty)
            {
                return Convert.ToBoolean(Result);
            }
            return DefaultValue;
        }
        /// <summary>
        /// 得到cookice值，以Int返回
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="DefaultValue">如果为空，返回此默认值</param>
        /// <returns></returns>
        public static int GetCookieAsInt(string key, int DefaultValue)
        {
            string Result = GetCookieValue(key);
            if (Result != string.Empty)
            {
                return Convert.ToInt32(Result);
            }
            return DefaultValue;
        }

        #endregion
    }
}